<template>
  <div style="background-image: url('../assets/img-1.jpg')">

    <div class="head">
      <b>
        模块化交通预测开源平台
      </b>
    </div>

    <div >
    <span class="mask bg-primary alpha-7"></span>
    <el-card class="login-form-layout">
      <el-form
          autocomplete="on"
          :model="loginForm"
          ref="loginForm"
          label-position="left"
          style="margin-bottom: -30px"
      >
        <div style="text-align: center">
          <!--    <svg-icon icon-class="login-mall" style="width: 56px;height: 56px;color: #409EFF"></svg-icon>-->
        </div>
        <h2 class="login-title color-main"
        style="font-size: 20px; font-family: 微软雅黑,serif;
              margin-top: 0px; margin-bottom: 20px">
          <b>
          用户登录
          </b>
        </h2>

        <!--用户名输入-->
        <el-form-item prop="username">
          <el-input
              name="username"
              type="text"
              v-model="loginForm.username"
              autocomplete="on"
              placeholder="请输入用户名"
          >
            <span slot="prefix">
            <!--  <svg-icon icon-class="user" class="color-main"></svg-icon> -->
            </span>
          </el-input>
        </el-form-item>

        <!--密码输入-->
        <el-form-item prop="password">
          <el-input
              name="password"
              :type="pwdType"
              @keyup.enter.native="handleLogin"
              v-model="loginForm.password"
              autocomplete="on"
              placeholder="请输入密码"
          >
            <span slot="prefix">
            <!--  <svg-icon icon-class="password" class="color-main"></svg-icon>-->
            </span>
            <span slot="suffix" @click="showPwd">
           <!--   <svg-icon icon-class="eye" class="color-main"></svg-icon> -->
            </span>
          </el-input>
        </el-form-item>

        <!--登录按键-->
        <el-form-item style="margin-bottom: 10px">
          <el-button
              style="width: 100%;
              background-color: #c1cbd7;"

              :loading="loading"
              @click.native.prevent="handleLogin"
          >登录</el-button>
        </el-form-item>

        <!--注册按键-->
        <el-form-item style="margin-bottom: 40px">
          <el-button
              style="width: 100%;
               background-color: #c1cbd7;"
              :loading="loading"
              @click.native.prevent="handleRegister"
          >注册</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    </div>

  </div>
</template>


<script>
export default {
  name: "login",
  data() {
    return {
      loginForm: {
        username: "",
        password: ""
      },
      loading: false,
      pwdType: "password",
    };
  },

  methods:
      {
        showPwd()
        {
          if (this.pwdType === "password") {
            this.pwdType = "";
          } else {
            this.pwdType = "password";
          }
        },

        handleLogin()
        {
          this.$refs.loginForm.validate(valid => {
            if (valid) {
              this.loading = true;
              this.$axios.get(`/user?username=${this.loginForm.username}&password=${this.loginForm.password}`)
                  .then(res => {
                    this.loading = false;
                    let code = res.data.code;
                    if (code == 200) {
                      console.log(res.data),
                      console.log(res.data.userId,res.data.username),
                      this.$router.push({
                        query: {userId: res.data.data.userId, username:res.data.data.username},
                        path: "/sys/users"
                      });
                    } else {
                      alert("登录失败，请重新登录")
                      this.$router.push({
                        path: "/"
                      });
                    }
                  })
                  .catch(() => {
                    alert("登录失败，请重新登录")
                    this.loading = false;
                  });
            } else {
              // eslint-disable-next-line no-console
              console.log("参数验证不合法！");
              return false;
            }
          });
        },

        handleRegister() {
          this.$router.push({
            path: "/register"
          });
        }
      }
};
</script>

<style scoped>
.login-form-layout {
  position: absolute;
  left: 0;
  right: 0;
  width: 360px;
  margin: 140px auto;
  border-top: 10px solid #8696a7;
}

.login-title {
  text-align: center;
}
.head {
  font-size: 40px;
  font-family: 微软雅黑 ,serif;
  align-content: center;
  display: flex;
  justify-content: center;
  align-items: center;
  padding-top: 5%;
  margin-bottom: -5%;
}
.login-center-layout {
  background: #409eff;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  margin-top: 200px;
}
</style>